package com.moyq5.permit.center.web.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.PageData;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.permit.center.bean.entity.SysAdmin;
import com.moyq5.permit.center.bean.search.SysAdmin4Search;
import com.moyq5.permit.center.feign.client.SysAdminFeign;
import com.moyq5.permit.center.web.ShiroCodeToken;
import com.moyq5.permit.center.web.ShiroLoginToken;
import com.moyq5.permit.center.web.jackson.filter.SysAdminFilter;

/**
 * 账号
 * @author Moyq5
 * @date 2019年7月15日
 */
@RestController
@RequestMapping("/admin")
public class SysAdminController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(SysAdminController.class);
	
	@Autowired
	private SysAdminFeign adminFeign;
	
	/**
	 * @author Moyq5
	 * @date 2019年8月13日
	 * @param admin
	 * @return
	 */
	@PostMapping(value="/login")
	@ResponseBody
	public StatusResult login(@RequestBody SysAdmin admin) {
		log.debug("账号登录>>>login(SysAdmin)");
		ShiroLoginToken token = new ShiroLoginToken();
		token.setName(admin.getName());
		token.setPwd(admin.getPwd());
		Subject subject = SecurityUtils.getSubject();
		subject.login(token);
		
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年8月13日
	 * @param name
	 * @param code
	 * @return
	 */
	@GetMapping(value="/auth")
	@ResponseBody
	public StatusResult auth(String name, String code) {
		log.debug("授权码登录>>>auth: {},{}", name, code);
		ShiroCodeToken token = new ShiroCodeToken();
		token.setName(name);
		token.setCode(code);
		Subject subject = SecurityUtils.getSubject();
		subject.login(token);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年8月14日
	 * @return
	 */
	@GetMapping(value="/logout")
	@ResponseBody
	public StatusResult logout() {
		log.debug("注销登录>>>logout：{}", getLoginAdmin().getName());
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月19日
	 * @param admin4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/!/**", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String page(SysAdmin4Search admin4s, PageData pd) throws JsonProcessingException {
		log.debug("查询账号信息>>>page(SysAdmin4Search, PageData)");
		return toJson(adminFeign.page(new Body<SysAdmin4Search, PageData>(admin4s, pd)), SysAdminFilter.get());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月22日
	 * @param admin
	 * @return
	 */
	@PutMapping("/add/!/**")
	@ResponseBody
	public ObjectResult<Long> add(@RequestBody SysAdmin admin) {
		log.debug("添加账号>>>add(SysAdmin)");
		return adminFeign.create(admin);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月22日
	 * @param admin
	 * @return
	 */
	@PatchMapping("/edit/!/**")
	@ResponseBody
	public StatusResult edit(@RequestBody @Validated SysAdmin admin) {
		log.debug("更新账号信息>>>edit(SysAdmin)");
		return adminFeign.update(admin);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月22日
	 * @param adminId
	 * @return
	 */
	@DeleteMapping("/del/!/**")
	@ResponseBody
	public StatusResult del(@RequestBody Long adminId) {
		log.debug("删除账号信息>>>del(adminId)");
		return adminFeign.remove(adminId);
	}
	
}
